
import Message from '../components/common/message/index';
import config from '../action/config.js'

export default function ({ store, redirect, app: { $axios } }) {
  // request拦截器，我这里设置了一个token，当然你可以不要
  $axios.onRequest((config) => {
    const auth = store.state.auth;
    if (auth.authenticated) {
      config.headers.Authorization = `Bearer ${auth.token}`
    }
  });
  $axios.onError((error) => {
    if(error.response.status === 401){
      Message({
        type:'warning',
        message:'用户登录信息已过期，请重新登录!'
      });
      if ( process.client ) {
        require('js-cookie').remove(config.default_data.KEY_COOKIE_TOKEN)
      }
      store.commit('userProfile/removeUser');
      store.commit('auth/logout');
      if (process.client) {
        redirect('/users/login');
      }
    }
    console.log(error)
  });

  $axios.interceptors.response.use((response) => {
      return response
  })
}
